﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TxValueStep
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.MultipleLocationsManipulation.ValueStepControl.Resources;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

public class TxValueStep : TxUserControl
{
  private const double DEFAULT_LINEAR_STEP_VALUE = 100.0;
  private const double DEFAULT_ANGULAR_STEP_VALUE = 0.17453292519943295;
  private const string RADIAN_SYMBOL = "ᶜ";
  private const string DEGREE_SYMBOL = "°";
  private double _linearStepValue;
  private double _angularStepValue;
  private TxValueStepType _type;
  private double _value;
  private bool _wasChangeHandled;
  private bool _fireChangeEvents = true;
  private IContainer components;
  private CUiContinuousButton btnRight;
  private TextBox tbValue;
  private TxLinkLabel hlStepSize;
  private CUiContinuousButton btnLeft;
  private ImageList ilSmallButtonsImages;
  private ToolTip ttMainTooltip;

  [Browsable(false)]
  public TxValueStepType Type
  {
    get => this._type;
    set
    {
      this._type = value;
      switch (this._type)
      {
        case TxValueStepType.Linear:
          this.Step = this._linearStepValue;
          break;
        case TxValueStepType.Angular:
          this.Step = this._angularStepValue;
          break;
      }
      this.UpdateTooltips();
      this.QuietlyUpdateValue(0.0);
    }
  }

  [Browsable(false)]
  public double Step
  {
    get
    {
      switch (this._type)
      {
        case TxValueStepType.Linear:
          return this._linearStepValue;
        case TxValueStepType.Angular:
          return this._angularStepValue;
        default:
          return 0.0;
      }
    }
    set
    {
      switch (this._type)
      {
        case TxValueStepType.Linear:
          this._linearStepValue = value;
          break;
        case TxValueStepType.Angular:
          this._angularStepValue = value;
          break;
      }
      this.InvalidateStepComponents();
      if (this.StepChanged == null || !this._fireChangeEvents)
        return;
      this.StepChanged((object) this, new EventArgs());
    }
  }

  [Browsable(false)]
  public double Value
  {
    get => this._value;
    set
    {
      double oldValue = this._value;
      this._value = value;
      int num = 2;
      if (!TxControlsUtility.DesignMode)
      {
        switch (this.Type)
        {
          case TxValueStepType.Linear:
            num = TxApplication.Options.Units.LinearDigitsAfterDecimalPoint;
            break;
          case TxValueStepType.Angular:
            num = TxApplication.Options.Units.AngularDigitsAfterDecimalPoint;
            break;
        }
      }
      this.tbValue.Text = this._value.ToString("F" + num.ToString());
      if (this.ValueChanged == null || !this._fireChangeEvents)
        return;
      this.ValueChanged((object) this, new TxValueStepValueChangeEventArg(oldValue, this._value));
    }
  }

  [Browsable(false)]
  public double LinearStep
  {
    get => this._linearStepValue;
    set
    {
      this._linearStepValue = value;
      if (this.Type != TxValueStepType.Linear)
        return;
      this.InvalidateStepComponents();
    }
  }

  [Browsable(false)]
  public double AngularStep
  {
    get => this._angularStepValue;
    set
    {
      this._angularStepValue = value;
      if (this.Type != TxValueStepType.Angular)
        return;
      this.InvalidateStepComponents();
    }
  }

  public TxValueStep() => this.InitializeComponent();

  public TxValueStep(double value, double step, TxValueStepType type)
  {
    this.InitializeComponent();
    if (TxControlsUtility.DesignMode)
      return;
    this.Type = type;
    this.Step = step;
    this.QuietlyUpdateValue(value);
  }

  private void TxValueStep_Load(object sender, EventArgs e)
  {
    if (TxControlsUtility.DesignMode)
      return;
    this.Type = TxValueStepType.Angular;
    this._angularStepValue = Math.PI / 18.0 / TxApplication.Options.Units.AngularMultiplier;
    this._linearStepValue = 100.0 / TxApplication.Options.Units.LinearMultiplier;
    if (((Control) this).Parent == null)
      return;
    ((Control) this).Parent.KeyDown += new KeyEventHandler(this.Parent_KeyDown);
  }

  private void Parent_KeyDown(object sender, KeyEventArgs e)
  {
    if (!this.tbValue.Focused || e.KeyCode != Keys.Return)
      return;
    this.ChangeValueAction();
    this._wasChangeHandled = true;
  }

  private void UpdateTooltips()
  {
    if (this.Type == TxValueStepType.Linear)
    {
      this.ttMainTooltip.SetToolTip((Control) this.btnLeft, TxValueStepResource.MoveOneStep);
      this.ttMainTooltip.SetToolTip((Control) this.btnRight, TxValueStepResource.MoveOneStep);
      this.ttMainTooltip.SetToolTip((Control) this.hlStepSize, TxValueStepResource.DefineTranslationStep);
    }
    else if (this.Type == TxValueStepType.Angular)
    {
      this.ttMainTooltip.SetToolTip((Control) this.btnLeft, TxValueStepResource.RotateOneStep);
      this.ttMainTooltip.SetToolTip((Control) this.btnRight, TxValueStepResource.RotateOneStep);
      this.ttMainTooltip.SetToolTip((Control) this.hlStepSize, TxValueStepResource.DefineRotationStep);
    }
    else
    {
      this.ttMainTooltip.SetToolTip((Control) this.btnLeft, string.Empty);
      this.ttMainTooltip.SetToolTip((Control) this.btnRight, string.Empty);
      this.ttMainTooltip.SetToolTip((Control) this.hlStepSize, string.Empty);
    }
  }

  public event TxValueStep.TxValueStepValueChangeEventHandler ValueChanged;

  public event TxValueStep.TxValueStepStepChangeEventHandler StepChanged;

  public event EventHandler ContinuousValueChangeStarted;

  public event EventHandler ContinuousValueChangeEnded;

  private void btnRight_ContinuousClick(object sender, EventArgs e) => this.Value += this.Step;

  private void btnLeft_ContinuousClick(object sender, EventArgs e)
  {
    this.Value += -1.0 * this.Step;
  }

  private void btn_ContinuousClickEnded(object sender, EventArgs e)
  {
    if (this.ContinuousValueChangeEnded == null || !this._fireChangeEvents)
      return;
    this.ContinuousValueChangeEnded((object) this, EventArgs.Empty);
  }

  private void btn_ContinuousClickStarted(object sender, EventArgs e)
  {
    if (this.ContinuousValueChangeStarted == null || !this._fireChangeEvents)
      return;
    this.ContinuousValueChangeStarted((object) this, EventArgs.Empty);
  }

  private void tbValue_KeyDown(object sender, KeyEventArgs e)
  {
    if (e.KeyCode == Keys.Return)
    {
      this.ChangeValueAction();
      this._wasChangeHandled = true;
    }
    else
      this._wasChangeHandled = false;
  }

  private void tbValue_Enter(object sender, EventArgs e) => this._wasChangeHandled = false;

  private void tbValue_Leave(object sender, EventArgs e)
  {
    if (this._wasChangeHandled)
      this._wasChangeHandled = false;
    else
      this.ChangeValueAction();
  }

  private void ChangeValueAction()
  {
    if (this._wasChangeHandled)
      return;
    double result = 0.0;
    if (double.TryParse(this.tbValue.Text, out result))
      this.Value = result;
    else
      this.QuietlyUpdateValue(this.Value);
  }

  public void QuietlyUpdateValue(double newVal)
  {
    if (TxControlsUtility.DesignMode)
      return;
    this._fireChangeEvents = false;
    this.Value = newVal;
    this._fireChangeEvents = true;
  }

  private void hlStepSize_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  {
    TxValueStepStepSizeDlg valueStepStepSizeDlg = new TxValueStepStepSizeDlg(this.Type, this.Step);
    if (((Form) valueStepStepSizeDlg).ShowDialog() != DialogResult.OK)
      return;
    this.Step = valueStepStepSizeDlg.StepValue;
  }

  private void InvalidateStepComponents()
  {
    string str = "";
    int num = 2;
    if (!TxControlsUtility.DesignMode)
    {
      switch (this._type)
      {
        case TxValueStepType.Linear:
          str = TxApplication.Options.Units.LinearUnitName;
          num = TxApplication.Options.Units.LinearDigitsAfterDecimalPoint;
          break;
        case TxValueStepType.Angular:
          num = TxApplication.Options.Units.AngularDigitsAfterDecimalPoint;
          str = TxApplication.Options.Units.AngularUnitName;
          break;
      }
    }
    ((Control) this.hlStepSize).Text = string.Format(TxValueStepResource.StepSize, (object) this.Step.ToString("F" + num.ToString()), (object) str);
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    this.components = (IContainer) new Container();
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (TxValueStep));
    this.btnRight = new CUiContinuousButton();
    this.ilSmallButtonsImages = new ImageList(this.components);
    this.tbValue = new TextBox();
    this.hlStepSize = new TxLinkLabel();
    this.btnLeft = new CUiContinuousButton();
    this.ttMainTooltip = new ToolTip(this.components);
    ((Control) this).SuspendLayout();
    ((Control) this.btnRight).Anchor = AnchorStyles.Top | AnchorStyles.Right;
    this.btnRight.ClickInterval = 200;
    ((ButtonBase) this.btnRight).ImageIndex = 1;
    ((ButtonBase) this.btnRight).ImageList = this.ilSmallButtonsImages;
    ((ButtonBase) this.btnRight).ImeMode = ImeMode.NoControl;
    ((Control) this.btnRight).Location = new Point(105, 20);
    ((Control) this.btnRight).Margin = new Padding(3, 3, 3, 0);
    ((Control) this.btnRight).Name = "btnRight";
    ((Control) this.btnRight).Size = new Size(24, 23);
    ((Control) this.btnRight).TabIndex = 19;
    ((ButtonBase) this.btnRight).UseVisualStyleBackColor = true;
    this.btnRight.ContinuousClick += new EventHandler(this.btnRight_ContinuousClick);
    this.btnRight.ContinuousClickStarted += new EventHandler(this.btn_ContinuousClickStarted);
    this.btnRight.ContinuousClickEnded += new EventHandler(this.btn_ContinuousClickEnded);
    this.ilSmallButtonsImages.TransparentColor = Color.Fuchsia;
    this.ilSmallButtonsImages.ColorDepth = ColorDepth.Depth32Bit;
    this.ilSmallButtonsImages.Images.Add(TxImageManager.GetImageByKey("ArrowLeft").ConvertTo<Image>(new Size()));
    this.ilSmallButtonsImages.Images.Add(TxImageManager.GetImageByKey("ArrowRight").ConvertTo<Image>(new Size()));
    this.ilSmallButtonsImages.Images.SetKeyName(0, "MoveLeft.png");
    this.ilSmallButtonsImages.Images.SetKeyName(1, "MoveRight.png");
    this.tbValue.AcceptsReturn = true;
    this.tbValue.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    this.tbValue.Location = new Point(32 /*0x20*/, 21);
    this.tbValue.Margin = new Padding(3, 3, 3, 0);
    this.tbValue.MinimumSize = new Size(67, 20);
    this.tbValue.Name = "tbValue";
    this.tbValue.Size = new Size(67, 20);
    this.tbValue.TabIndex = 18;
    this.tbValue.WordWrap = false;
    this.tbValue.Enter += new EventHandler(this.tbValue_Enter);
    this.tbValue.KeyDown += new KeyEventHandler(this.tbValue_KeyDown);
    this.tbValue.Leave += new EventHandler(this.tbValue_Leave);
    ((Control) this.hlStepSize).Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    ((Control) this.hlStepSize).AutoSize = true;
    ((Label) this.hlStepSize).ImeMode = ImeMode.NoControl;
    ((Control) this.hlStepSize).Location = new Point(20, 4);
    ((Control) this.hlStepSize).Name = "hlStepSize";
    ((Control) this.hlStepSize).Size = new Size(90, 13);
    ((Control) this.hlStepSize).TabIndex = 16 /*0x10*/;
    ((LinkLabel) this.hlStepSize).TabStop = true;
    ((Control) this.hlStepSize).Text = "Step size: 10.00 °";
    ((Label) this.hlStepSize).TextAlign = ContentAlignment.MiddleCenter;
    ((LinkLabel) this.hlStepSize).LinkClicked += new LinkLabelLinkClickedEventHandler(this.hlStepSize_LinkClicked);
    this.btnLeft.ClickInterval = 200;
    ((ButtonBase) this.btnLeft).ImageIndex = 0;
    ((ButtonBase) this.btnLeft).ImageList = this.ilSmallButtonsImages;
    ((ButtonBase) this.btnLeft).ImeMode = ImeMode.NoControl;
    ((Control) this.btnLeft).Location = new Point(2, 20);
    ((Control) this.btnLeft).Margin = new Padding(3, 3, 3, 0);
    ((Control) this.btnLeft).Name = "btnLeft";
    ((Control) this.btnLeft).Size = new Size(24, 23);
    ((Control) this.btnLeft).TabIndex = 17;
    ((ButtonBase) this.btnLeft).UseVisualStyleBackColor = true;
    this.btnLeft.ContinuousClick += new EventHandler(this.btnLeft_ContinuousClick);
    this.btnLeft.ContinuousClickStarted += new EventHandler(this.btn_ContinuousClickStarted);
    this.btnLeft.ContinuousClickEnded += new EventHandler(this.btn_ContinuousClickEnded);
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Inherit;
    ((Control) this).Controls.Add((Control) this.btnLeft);
    ((Control) this).Controls.Add((Control) this.btnRight);
    ((Control) this).Controls.Add((Control) this.tbValue);
    ((Control) this).Controls.Add((Control) this.hlStepSize);
    ((Control) this).Margin = new Padding(3, 3, 3, 0);
    ((Control) this).MinimumSize = new Size(131, 43);
    ((Control) this).Name = nameof (TxValueStep);
    ((Control) this).Size = new Size(131, 43);
    ((UserControl) this).Load += new EventHandler(this.TxValueStep_Load);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }

  public delegate void TxValueStepValueChangeEventHandler(
    object sender,
    TxValueStepValueChangeEventArg args);

  public delegate void TxValueStepStepChangeEventHandler(object sender, EventArgs args);
}
